package com.qm.console.es.dto;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qm.console.es.do_.EsViewDO;
import com.qm.console.es.do_.EsViewDOWithBLOBs;

import java.util.List;

import lombok.Getter;
import lombok.Setter;

/**
 * //                       _oo0oo_
 * //                      o8888888o
 * //                      88" . "88
 * //                      (| -_- |)
 * //                      0\  =  /0
 * //                    ___/`---'\___
 * //                  .' \\|     |// '.
 * //                 / \\|||  :  |||// \
 * //                / _||||| -:- |||||- \
 * //               |   | \\\  -  /// |   |
 * //               | \_|  ''\---/''  |_/ |
 * //               \  .-\__  '-'  ___/-. /
 * //             ___'. .'  /--.--\  `. .'___
 * //          ."" '<  `.___\_<|>_/___.' >' "".
 * //         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 * //         \  \ `_.   \_ __\ /__ _/   .-` /  /
 * //     =====`-.____`.___ \_____/___.-`___.-'=====
 * //                       `=---='
 * //
 * //
 * //     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * //
 * //               佛祖保佑         永无BUG
 * <p>
 * com.qm.console.es.dto
 * Created on 2021/7/5.
 */
@Getter
@Setter
public class EsViewDTO {

   private Integer id;
   private String name;
   private String desc;
   private JSONObject requestBody;
   private JSONObject filters;
   private List<FieldConfigDTO> fields;

   public EsViewDOWithBLOBs toDo() {

      EsViewDOWithBLOBs result = new EsViewDOWithBLOBs();
      result.setId(id);
      result.setName(name);
      result.setDesc(desc);
      if (requestBody != null) {
         result.setRequestBody(requestBody.toJSONString());
      }
      if (filters != null) {
         result.setFilters(filters.toJSONString());
      }
      if (fields != null) {
         result.setFields(JSON.toJSONString(fields));
      }
      return result;
   }

   public static EsViewDTO fromDo(EsViewDOWithBLOBs item) {
      EsViewDTO result = new EsViewDTO();
      result.setId(item.getId());
      result.setName(item.getName());
      result.setDesc(item.getDesc());
      if (item.getFields() != null) {
         result.setFields(JSONArray.parseArray(item.getFields(), FieldConfigDTO.class));
      }
      if (item.getFilters() != null) {
         result.setFilters(JSONObject.parseObject(item.getFilters()));
      }
      if (item.getRequestBody() != null) {
         result.setRequestBody(JSONObject.parseObject(item.getRequestBody()));
      }
      return result;

   }

   public static EsViewDTO fromDo(EsViewDO item) {
      EsViewDTO result = new EsViewDTO();
      result.setId(item.getId());
      result.setName(item.getName());
      result.setDesc(item.getDesc());
      return result;
   }

}
